package top.firejames.io;

import top.firejames.bean.SliceFile;
import top.firejames.io.thread.ThreadIo;
import top.firejames.util.FileConstant;
import top.firejames.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @auther 夏浩
 * @creat 2021-09-2021/9/24-9:51
 */
public class SortTest {
    static int count = 0;
    //线程数量
    static int threadNumber = 10;
    //切割的文件放入的文件夹
    static String destPath = "txt";
    //切割完成的路径
    static String[] fileNames = null;

    static List<SliceFile> list = new CopyOnWriteArrayList<>();

    static long start = System.currentTimeMillis();

    public synchronized static void addCount() {
        count++;
    }


    public static void main(String[] args) {


        String startPath = "mergeNumbers.txt";
        //删除已有的文件
        delAllFiles();
        //拿到分割完成的文件的路径
        fileNames = ThreadIo.readAndWrite(startPath, destPath, 1000000, threadNumber);
        String s = Arrays.toString(fileNames);
        System.out.println("数组"+s);

    }


    public static void delAllFiles() {
        File file = new File(destPath);
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                if (file1 != null) {
                    file1.delete();
                }
            }
        }
        file.delete();
    }

    public static void afterCut() {
        final ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
        System.out.println("开始排序并输出");
        for (int i = 0; i < threadNumber; i++) {
            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //每个线程从分割出来的文件中读取数据到数组ints中
                        int[] ints = FileUtil.LineNumberReadBySort(fileNames[finalI]);
                        System.out.println("ints"+ints.length);
                        System.out.println("开始排序：" + fileNames[finalI]);
                        //每个线程把ints数组进行归并排序
                        Sort.mergeSort(ints, 0, ints.length - 1);
                        System.out.println("排序完成：" + fileNames[finalI] + "，开始写入");
                        StringBuffer stringBuffer = new StringBuffer(10000);
                        for (int j = 1; j <= ints.length; j++) {
                            stringBuffer.append(ints[j-1]).append("\n");
                            if (j % 10000 == 0) {
                                if (j == ints.length) {
                                    //如果是最后一行就不用输出换行
                                    stringBuffer.delete(stringBuffer.length() -1, stringBuffer.length());
                                }
                                //每个线程把stringBuffer写入到文件中，每次写入一行
                                FileUtil.write(fileNames[finalI], stringBuffer.toString());
                                //每写一次，就清空stringBuffer
                                stringBuffer.delete(0, stringBuffer.length());
                            }
                        }
                        //获取到文件名
                        String fileName = fileNames[finalI].substring(fileNames[finalI].indexOf("\\") + 1,
                                fileNames[finalI].length() - FileConstant.FILE_SUFFIX.length());

                        SliceFile sliceFile = new SliceFile(fileName, ints.length);
                        list.add(sliceFile);
                        addCount();
                        System.out.println("写入完成：" + fileNames[finalI]);
                        if (count >= threadNumber) {
                            executorService.shutdown();
                            list.forEach(bean -> {
                                System.out.println(bean);
                            });
                            long l = System.currentTimeMillis() - start;
                            System.out.println("耗时：" + l + "毫秒");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

}
